/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2021-08-24 07:28:55
 * @LastEditors: wangzhilei
 * @LastEditTime: 2021-09-04 00:59:15
 */
#include <stdlib.h>
#include <stdio.h>

#define HTTP 1
#define TCP 2
#define UDP 3
#define UART 4

#define INUSE 1
#define UNUSE 0

typedef unsigned int u32;
typedef unsigned char u8;
typedef unsigned short u16;

extern int errno;

typedef struct db_data
{
	u8 id;
	u8 flag;
	int sock_fd;
	u8 *send_buffer;
	u32 (*Init)(struct db_data *pdata);
	int (*SendFunction)(struct db_data *pdata, void *sendbuffer);
} DB_DATA_T;
typedef DB_DATA_T *DB_DATA_PT;

typedef struct db_list
{
	u32 limit_size;
	DB_NODE_PT head;
	DB_NODE_PT tail;
} DB_LIST_T;
typedef DB_LIST_T *DB_LIST_PT;

typedef struct db_node
{
	void *data;
	struct db_node *prev;
	struct db_node *next;

} DB_NODE_T;
typedef DB_NODE_T *DB_NODE_PT;

/**
 * @name:proxy_list_create 
 * @msg:proxy list create 
 * @param {void}
 * @return {DB_LIST_PT}
 */
DB_LIST_PT proxy_list_create(void)
{
	DB_LIST_PT list_head = NULL;
	list_head = (DB_LIST_PT)malloc(sizeof(DB_LIST_T));
	if (list_head == NULL)
	{
		errno = ENOMEN;
		return NULL;
	}
	list_head->limit_size = 0;
	list_head->head = (DB_NODE_PT)malloc(sizeof(DB_NODE_T));
	if (list_head->head == NULL)
	{
		errno = ENOMEN;
		return NULL;
	}
	list_head->head->next = list_head->head->prev = NULL;
	list_head->head->data = NULL;
	list_head->tail = list_head->head;
	return list_head;
}
/**
 * @name:proxy_list_insert_before 
 * @msg: 在某个结点前插入新结点
 * @param {DB_LIST_T **list_head,int num,void *new_node}
 * @return {int}
 */
int proxy_list_insert_before(DB_LIST_T **list_head, int num, void *new_node_data)
{
	int counter = 1;
	DB_NODE_PT current = NULL;
	DB_NODE_PT new_node = NULL;

	if (*list_head == NULL || list_head == NULL)
	{
		errno = EINVAL;
		return -1;
	}
	if ((*list_head)->limit_size != 0)
	{
		new_node = (DB_NODE_PT)malloc(sizeof(DB_NODE_T));
		if (new_node == NULL)
		{
			errno = ENOMEM;
			return -1;
		}
		new_node->data = new_node_data;
		new_node->next = new_node->prev = NULL;
		if (num > 0 && num < (*list_head)->limit_size)
		{
			current = (*list_head)->head;
			while (counter < num)
			{
				counter++;
				current = current->next;
			}
		}
		//插入节点
		if (counter == 1)
		{
			(*list_head)->head->prev = new_node;
			new_node->next = (*list_head)->head;
			(*list_head)->head = new_node;
			(*list_head)->limit_size++;
			return 0;
		}
		new_node->prev = current->prev;
		new_node->next = current;
		current->prev->next = new_node;
		current->prev = new_node;
		(*list_head)->limit_size++;
		return 0;
	}
	else
	{

		if (num != 0)
		{
			errno = EINVAL;
			return -1;
		}
		(*list_head)->head->data = new_node;
		(*list_head)->limit_size++;
		return 0;
	}
}
/**
 * @func: core_list_insert_after
 * @msg: 某个节点后插入新节点
 * @param {DB_LIST_T **list_head, int num, void *new_node_data}
 * @return {int}
 */
int core_list_insert_after(DB_LIST_T **list_head, int num, void *new_node_data)
{
	int counter = 1;
	DB_NODE_PT current = NULL;
	DB_NODE_PT new_node = NULL;

	if (*list_head == NULL || list_head == NULL)
	{
		errno = EINVAL;
		return -1;
	}
	if ((*list_head)->limit_size != 0)
	{
		new_node = (DB_NODE_PT)malloc(sizeof(DB_NODE_T));
		if (new_node == NULL)
		{
			errno = ENOMEM;
			return -1;
		}
		new_node->data = new_node_data;
		new_node->next = new_node->prev = NULL;
		if (num > 0 && num < (*list_head)->limit_size)
		{
			current = (*list_head)->head;
			while (counter < num)
			{
				counter++;
				current = current->next;
			}
			if (current->next == NULL)
			{
				(*list_head)->tail = new_node;
				current->next = new_node;
				new_node->prev = current;
				(*list_head)->limit_size++;
				return 0;
			}
			new_node->prev = current;
			new_node->next = current->next;
			current->next = new_node;
			current->next->prev = new_node;
			(*list_head)->limit_size++;
			return 0;
		}
		else
		{
			errno = EINVAL;
			free(new_node);
			new_node = NULL;
			return -1;
		}
	}
	else
	{
		if (num != 0)
		{
			errno = EINVAL;
			return -1;
		}
		(*list_head)->head->data = new_node;
		(*list_head)->limit_size++;
		return 0;
	}
}
/**
 * @func: core_list_delete
 * @msg: 删除某个节点
 * @param {DB_LIST_T **list_head, int num}
 * @return {int}
 */
int core_list_delete(DB_LIST_T **list_head, int num)
{
	int counter = 1;
	DB_NODE_PT current = NULL;
	DB_NODE_PT tmp = NULL;
	if (*list_head == NULL || list_head == NULL)
	{
		errno = EINVAL;
		return -1;
	}
	current = (*list_head)->head;
	while (counter < num)
	{
		counter++;
		current = current->next;
	}
	if (counter == 1)
	{
		tmp = (*list_head)->head;
		(*list_head)->head = (*list_head)->head->next;
		free(tmp);
		tmp = NULL;
		(*list_head)->head->prev = NULL;
		(*list_head)->limit_size--;
		return 0;
	}
	if ((*list_head)->limit_size == counter)
	{
		tmp = (*list_head)->tail;
		(*list_head)->tail = (*list_head)->tail->prev;
		free(tmp);
		tmp = NULL;
		(*list_head)->tail->next = NULL;
		(*list_head)->limit_size--;
		return 0;
	}
	tmp = current;
	current->prev->next = current->next;
	current->next->prev = current->prev;
	free(tmp);
	tmp = NULL;
	(*list_head)->limit_size--;
	return 0;
}
/**
 * @func: core_list_modify
 * @msg: 修改某个节点
 * @param {DB_LIST_T **list_head, int num,void *new_node_data}
 * @return {int}
 */
int core_list_modify(DB_LIST_T **list_head, int num, void *new_node_data)
{
	int counter = 0;
	DB_NODE_PT current = NULL;
	if (*list_head == NULL || list_head == NULL || num < 0 || num > (*list_head)->limit_size)
	{
		errno = EINVAL;
		return -1;
	}
	current = (*list_head)->head;
	while (counter != num - 1)
	{
		counter++;
		current = current->next;
	}
	current->data = new_node_data;
	return 0;
}
/**
 * @func: core_list_show
 * @msg: 查看双向链表节点
 * @param { u32 num}
 * @return {DB_NODE_T **}
 */
DB_NODE_T **core_list_show(u32 num)
{
}
/**
 * @func: core_list_cuid
 * @msg: 遍历链表
 * @param {DB_NODE_T **list_head,void *(do_function)(void *)}
 * @return {void}
 */
void core_list_cuid(DB_LIST_T *list_head, void *(do_function)(void *))
{
	u32 i = 0;
	if (list_head == NULL || list_head->limit_size < 0)
	{
		errno = EINVAL;
		return -1;
	}
	for (i = 0; i < list_head->limit_size; i++)
	{
		(*do_function)(__core_list_visit(&list_head, i));
	}
}
/**
 * @func: core_list_visit
 * @msg: 显示节点数据
 * @param {u32 num}
 * @return {void*}
 */
static inline void *__core_list_visit(DB_LIST_T **list_head, u32 num)
{
	int counter = 0;
	DB_NODE_PT current = (*list_head)->head;
	if (*list_head == NULL || list_head == NULL || num < 0 || num > (*list_head)->limit_size)
	{
		errno = EINVAL;
		return -1;
	}
	for (counter = 0; counter < num; counter++)
	{
		current = current->next;
	}
	return (void *)current->data;
}
/**
 * @func: core_list_search
 * @msg: 查找节点
 * @param {DB_LIST_T **list_head,void * find_data,int(*compare)(void*,void*)}
 * @return {int}
 */
int core_list_search(DB_LIST_T **list_head, void *find_data, int (*compare)(void *, void *))
{
		int counter = 1;
	DB_NODE_PT current = (*list_head)->head;
	if (*list_head == NULL || list_head == NULL)
	{
		errno = EINVAL;
		return -1;
	}
	current=(*list_head)->head;
	while (compare(current->data,find_data)!=0&&current->next!=NULL)
	{
		current=current->next;
		counter++;
	}
	if(current->next==NULL&&compare(current->data,find_data)!=0)
		return 0;
		return counter;
	
}
